<?php
/**
 * 匠言知识付费
 * Author: 山西匠言网络科技有限公司
 * This is not a free software, it under the license terms, you can visit https://www.zsffzxkc.cn/ get more details.
 */
namespace app\index\controller;
use think\Controller;
use think\Db;
class Vip extends  Base
{
    public function viptimeinfo(){
        $id=input('post.id');
        $map=array(
          'acid'=>1,
        );
        $data=Db::name('viptime')
            ->where('id',$id)
            ->where($map)
            ->find();
        $this->result(0,'获取vip数据成功',$data,0,'json');
    }
    public function jhmjh(){
        $post=input('post.');
        $map=array(
            'acid'=>1,
        );
        $jhm=db('jhm')->where($map)->where('num',$post['jhm'])->find();
        if(empty($jhm)){
            $this->result(1,'激活码不正确,请重试','',0,'json');
        }else{
            $pici=db('jhm_pici')->where($map)->where('id',$jhm['pici_id'])->find();
            $data=array(
                'is_vip'=>1,
                'vip_state'=>'正常',
                'viptypeid'=>$pici['viptimeid'],
                'vipsj_fangshi'=>'jhm',
                'vipsj_time'=>time()
            );
            $res=db('user')->where($map)->where('id',$post['uid'])->update($data);
            if($res){
                $res1=db('jhm')->where($map)->where('num',$post['jhm'])->update(['uid'=>$post['uid'],'hexiaotime'=>time(),'is_jh'=>1]);
                if($res1){
                    $this->result(0,'激活成功','',0,'json');
                }
            }
        }
    }

    public function vipinfo() {
        $post=input('post.');
        $map=array(
            'acid'=>1,
        );
        $data = db('user')->where($map)->where(['id'=>$post['uid']])->find();
        if(!empty($data) && $data['viptypeid'] !='' && $data['viplevelid'] !=''){
            if($data['vipsj_orderid']!=''){
                $data1=db('mediaorder')->where($map)->where('orderid',$data['vipsj_orderid'])->find();
            }else{
                $data1['ordertime'] = 0;
            }
            $data2=db('viptime')->where($map)->where('id',$data['viptypeid'])->find();
            $viplevelinfo=db('viplevel')->where($map)->where('id',$data['viplevelid'])->find();
            $starttime=$data['vipsj_time']?$data['vipsj_time']:$data1['ordertime'];
            if($data2['days']!=0){
                if(time() > ($starttime + (60*60*24*$data2['days']))){
                    $newdata=array(
                        'is_vip'=>0,
                        'vip_state'=>'过期',
                        'viplevelid'=>0,
                        'vipsj_fangshi'=>'',
                        'vipsj_orderid'=>'',
                        'vipsj_time'=>'',
                        'viptypeid'=>0
                    );
                    $res=db('user')->where($map)->where(['id'=>$post['uid']])->update($newdata);
                    $userinfo=db('user')->where(['acid'=>1,'id'=>$post['uid']])->find();
                    unset($userinfo['password']);
                    $data3=array(
                        'viptype'=>$data2['name'],
                        'dqtime'=>date('Y-m-d',$starttime + (60*60*24*$data2['days'])),
                        'userinfo'=>$userinfo
                    );
                    if($res){
                        $this->result(2,'VIP已到期',$data3,0,'json');
                    }
                }else{
                    $userinfo=db('user')->where(['acid'=>1,'id'=>$post['uid']])->find();
                    unset($userinfo['password']);
                    $data3=array(
                        'viptype'=>$data2['name'],
                        'levelname'=>$viplevelinfo['name'],
                        'dqtime'=>date('Y-m-d',$starttime + (60*60*24*$data2['days'])),
                        'userinfo'=>$userinfo
                    );
                    $this->result(0,'已经是'.$data2['name'],$data3,0,'json');
                }
            }else{
                $this->result(1,'还不是VIP','',0,'json');
            }
        }
        else{
            $this->result(1,'还不是VIP','',0,'json');
        }
    }
}
